package com.openxt.nbzj.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.openxt.nbzj.R;
import com.openxt.nbzj.call.HeartBeatManager;
import com.openxt.nbzj.fragment.OpenxtBaseActivity;
import com.openxt.nbzj.model.ConfigModel;
import com.openxt.nbzj.model.RemotingModel;
import com.openxt.nbzj.remoting.ClientEx;
import com.openxt.nbzj.remoting.RemotingEx;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.fastjson.JSONObject;
import com.openxt.nbzj.util.StringUtil;

import qs.openxt.libs.progress.Wait;
import qs.openxt.libs.util.ActivityUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016/7/5.
 */
public class LoginActivity extends OpenxtBaseActivity implements View.OnClickListener, RemotingEx.OnActionListener {

    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.login_btn)
    Button login_btn;
    @BindView(R.id.remember_password)
    CheckBox remember_password;
    @BindView(R.id.forget_password)
    LinearLayout forget_password;
    @BindView(R.id.register)
    LinearLayout register;
    @BindView(R.id.btn_back)
    LinearLayout btnBack;

    RemotingModel remotingModel = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);
        ButterKnife.bind(this);
        setActionUrl("#/login");

        remotingModel = new RemotingModel(this);

        String rp = Utils.getLocalStorage(this, ConfigModel.REMEMBER_PASSWORD);
        if (StringUtil.isNull(rp))
            rp = "true";

        if ("true".equals(rp))
            remember_password.setChecked(true);
        else
            remember_password.setChecked(false);

        username.setText(Utils.getLocalStorage(this, ConfigModel.LOGIN_USERNAME));
        password.setText(Utils.getLocalStorage(this, ConfigModel.LOGIN_PASSWORD));

        login_btn.setOnClickListener(this);
        forget_password.setOnClickListener(this);
        register.setOnClickListener(this);
        btnBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_back){
            finish();
            return;
        }
        if (!ActivityUtil.isNetworkAvailable(this)) {
            showMsg("无网络，请先查看网络是否正常打开");
            return;
        }
        if (v.getId() == R.id.login_btn) {
            if (StringUtil.isNull(username.getText().toString())) {
                showMsg("请输入账户或手机号码");
                return;
            }
            if (StringUtil.isNull(password.getText().toString())) {
                showMsg("请输入登录密码");
                return;
            }
            remotingModel.login(username.getText().toString(), password.getText().toString(), this, true);
        } else if (v.getId() == R.id.forget_password) {
            Utils.setLocalStorage(this, ConfigModel.NATIVE_PAGENAME, getActionUrl());
            Bundle bundle = new Bundle();
            String RedirectURL = "index.html#/forgetpwd";
            bundle.putString("url", ConfigModel.ROOTPATH + RedirectURL);
            toActivity(WebViewActivity.class, bundle, false);
        } else if (v.getId() == R.id.register) {
            Utils.setLocalStorage(this, ConfigModel.NATIVE_PAGENAME, getActionUrl());
            Bundle bundle = new Bundle();
            String RedirectURL = "index.html#/register";
            bundle.putString("url", ConfigModel.ROOTPATH + RedirectURL);
            toActivity(WebViewActivity.class, bundle, false);
        }
    }

    @Override
    public void onActionCompleted(String reVal, String actionName) {
        String $username = username.getText().toString();
        Utils.setLocalStorage(this, ConfigModel.REMEMBER_PASSWORD, String.valueOf(remember_password.isChecked()));
        Utils.setLocalStorage(this, ConfigModel.HOME_LOADING, "false");
        Utils.setLocalStorage(this, ConfigModel.LOGIN_USERNAME, $username);
        if (remember_password.isChecked())
            Utils.setLocalStorage(this, ConfigModel.LOGIN_PASSWORD, password.getText().toString());
        else
            Utils.removeLocalStorage(this, ConfigModel.LOGIN_PASSWORD);
        Utils.setLocalStorage(this, actionName, reVal);
        try {
            JSONObject data = JSON.parseObject(reVal);
            ClientEx.CUSTOMERID = StringUtil.doubleToLString(data.getString("USERID"));
            /*//取住建委用户
            JSONArray IDENTITIES = data.getJSONArray("IDENTITIES");
            for (int i = 0, j = IDENTITIES.size(); i < j; i++) {
                JSONObject item = IDENTITIES.getJSONObject(i);
                if("63".equals(item.getString("APPID")) && item.getBooleanValue("ISMR")) {
                    ClientEx.CUSTOMERID = StringUtil.doubleToLString(item.getString("IDENTITYID"));
                    break;
                }
            }*/

            Utils.setLocalStorage(this, ClientEx.CUSTOMERID_KEY, ClientEx.CUSTOMERID);

            try{
                JSONObject json = JSON.parseObject(reVal);
                HeartBeatManager.instance().start(this, json.getInteger("HeartBeatMinutes"));
            }catch (Exception e){
            }
            setResult(RESULT_OK);
            finish();
        } catch (Exception e) {
        }
    }

    @Override
    public void onActionFailed(String errorVal, String actionName) {
        Wait.sharedInstance().stopAll();
        showMsg(errorVal);
    }
}